import http from "Http"
import tips from "Tips"
const common = {
    user: function (that, cb) {
        var user = wx.getStorageSync('user');
        console.log(user)
        if (!user || !user.unid) {
            wx.getSetting({
                success: function (res) {
                    if (res.authSetting['scope.userInfo']) {
                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                        //shouquan == 2 表示 弹出请等待.. 页面
                        that.setData({
                            shouquan: 2
                        })
                        wx.login({
                            success: function (res) {
                                if (res.code) {
                                    var udata = {
                                        code: res.code
                                    }
                                    http.req('/Wxapi/getOpenId', udata, function (res) {
                                        var edata = {
                                            openid: res.openid,
                                        }
                                        if (res) {
                                            wx.getUserInfo({
                                                success: function (userres) {
                                                    edata.wx_name = userres.userInfo.nickName
                                                    edata.wx_img = userres.userInfo.avatarUrl
                                                    var unid_data = {
                                                        session_key: res.session_key,
                                                        encryptedData: userres.encryptedData,
                                                        iv: userres.iv
                                                    }
                                                    http.req('/Wxapi/getUserinfo', unid_data, function (unres) {
                                                        edata.unid = unres.unionId
                                                        console.log(edata)
                                                        http.req('/Wxapi/add_user', edata, function (res) {
                                                            console.log(res)
                                                            edata.userid = res
                                                            wx.setStorageSync('user', edata);
                                                            return typeof cb == "function" && cb(edata)
                                                        })
                                                    })


                                                }
                                            })
                                        }

                                    });
                                }
                            },

                        });
                    } else {
                        that.setData({
                            shouquan: 1
                        })
                    }
                }
            })

        }


    },


}
export default common;